package test.hide;

import java.util.ArrayList;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ContentProvider;
import android.content.ContentProviderOperation;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.OperationApplicationException;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.RemoteException;
import android.provider.CallLog;
import android.provider.Contacts;
import android.provider.Contacts.People;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.view.View;
import android.widget.Button;
import android.widget.RemoteViews.ActionException;
import android.widget.Toast;

public class HiddenNumberActivity extends Activity {

	Button call, delete;
	String tempNum;
	final String fittizio = "8080";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		tempNum = "1234";

		call = (Button) findViewById(R.id.call);
		delete = (Button) findViewById(R.id.deleteLast);

		call.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(Intent.ACTION_CALL);
				intent.setData(Uri.parse("tel: " + tempNum));
				startActivity(intent);
			}
		});

		delete.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				ContentValues values = new ContentValues();
				values.put(CallLog.Calls.CACHED_NAME, "Hidden Number");
				Uri calllog = Uri.parse("content://call_log/calls");
				ContentResolver provider = HiddenNumberActivity.this
						.getContentResolver();
				Cursor c = provider.query(calllog, null, null, null, null);
				c.moveToLast();
				String idname = "_id";
				String numberName = "number";
				int last = c.getInt(c.getColumnIndex(idname));
				String where = " " + numberName + " = ? ";
				String[] whereArgs = { String.valueOf(tempNum) };
				int del = provider.delete(calllog, where, whereArgs);
				System.out.println(del);
			}
		});

	}

	@Override
	protected void onResume() {
		super.onResume();
		ContentValues values = new ContentValues();
		values.put(CallLog.Calls.CACHED_NAME, "Hidden Number");
		Uri calllog = Uri.parse("content://call_log/calls");
		ContentResolver provider = this.getContentResolver();
		String numberName = "number";
		String where = " " + numberName + " = ? ";
		String[] whereArgs = { String.valueOf(tempNum) };
		int del = provider.delete(calllog, where, whereArgs);
		System.out.println("Dal registro chiamate eliminati:" + del);

		Uri phone = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
		where = " " + Phone.NUMBER + " = ? ";
		whereArgs[0] = String.valueOf(tempNum);
		String[] projection = { Phone._ID, Phone.CONTACT_ID,
				Phone.DISPLAY_NAME, Phone.NUMBER };
		Cursor phoneCurs = provider.query(phone, projection, where, whereArgs,
				null);
		if (!phoneCurs.isAfterLast()) {
			phoneCurs.moveToFirst();
//			String numero = phoneCurs.getString(phoneCurs
//					.getColumnIndex(Phone.NUMBER));
//			String id = phoneCurs
//					.getString(phoneCurs.getColumnIndex(Phone._ID));
			String contactId = phoneCurs.getString(phoneCurs
					.getColumnIndex(Phone.CONTACT_ID));
			String name = phoneCurs.getString(phoneCurs
					.getColumnIndex(Phone.DISPLAY_NAME));

			ContentResolver cr = getContentResolver();

			where = ContactsContract.Data.CONTACT_ID + " = ? ";
			String[] params = new String[] { contactId };

			Cursor phoneCur = managedQuery(ContactsContract.Data.CONTENT_URI,
					null, where, params, null);

			ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

			if (phoneCur != null) {
				ops.add(ContentProviderOperation
						.newUpdate(ContactsContract.Data.CONTENT_URI)
						.withSelection(where, params)
						.withValue(ContactsContract.CommonDataKinds.Phone.DATA,
								fittizio).build());
			}

			phoneCur.close();

			try {
				cr.applyBatch(ContactsContract.AUTHORITY, ops);
			} catch (RemoteException e) {
				e.printStackTrace();
			} catch (OperationApplicationException e) {
				e.printStackTrace();
			}
			Toast.makeText(HiddenNumberActivity.this, "Modificato: " + name,
					Toast.LENGTH_SHORT).show();
		}

	}
}